<template>
	<view></view>
</template>
<script>
	const url = 'https://test.recharge.607068.com';
	const systeminfo = uni.getSystemInfoSync(); //获取设备信息 
	export default {
		onLaunch: function() {
			console.log('onLaunch');
			//添加tabbar中间按钮监听事件
			uni.onTabBarMidButtonTap(() => {
				uni.navigateTo({
					url: "/pages/release/release",
				})
			})

			//网络监听
			uni.onNetworkStatusChange(res=> {
				console.log(res)
				if(!res.isConnected){
					getApp().globalData.alert('网络异常，请检测你的网络状态！')
				}
			})
		
			//h5页面唤起监听
			let args = plus.runtime.arguments;
			console.log(args)
			if (args.indexOf('ttyl://') != -1) {
				let ttylurl = args.replace('ttyl://', '');
				//console.log(ttylurl);
				setTimeout(() => {
					uni.navigateTo({
						url: '/' + ttylurl
					})
				}, 1000)
			}

			//安卓底部虚拟按键背景色修改方式
			if (plus.os.name == 'Android') {
				var Color = plus.android.importClass("android.graphics.Color");
				plus.android.importClass("android.view.Window");
				var mainActivity = plus.android.runtimeMainActivity();
				var window_android = mainActivity.getWindow();
				window_android.setNavigationBarColor(Color.BLACK);
			}

			//接受app推送通知 
			plus.push.addEventListener('receive', (msg) => {
				//console.log(msg);
				this.pushnav(msg)
			}, false)
			//接受APP点击接受消息
			plus.push.addEventListener('click', msg => {
				//console.log(msg);
				this.pushnav(msg)
			}, false)
			//加载全局规则配置信息
			this.config()
			//清除角标
			this.clearsubscript()
			//是否开启通知
			//this.isNotificationsEnabled()
			this.CheckTabelMessage()
		},

		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			//检测用户消息表是否创建
			CheckTabelMessage() {
				this.$sqliteDb.openSqlite().then(res => {
					let sql ='create table if not exists message("id" INTEGER PRIMARY KEY AUTOINCREMENT,"member_id" INTEGER,"title" varchar(50),"content" TEXT,"is_read" INTEGER DEFAULT 0,"created_at" TEXT)'
					this.$sqliteDb.executeSql(sql).then(ret => {
						this.$sqliteDb.closeSQL()
					}).catch(e => {
					})
				}).catch(error => {
					this.$ShowMsg('sqllite_error::' + JSON.stringify(error))
				})
			},

			//清除app角标
			clearsubscript() {
				plus.runtime.setBadgeNumber(0)
			},

		
			//加载全局配置信息
			config() {
				this.$HttRequest({
					url: '/index/rule',
					param: ''
				}).then((res) => {
					if (res.status === 10000) {
						console.log(res.data)
						getApp().globalData.ruleconfig = res.data 
					} else {
						this.$ShowMsg(res.message)
					}
				}).catch((error) => {
					//异常错误
					this.$ShowMsg('statusCode：' + error.errMsg)
				})
			},

			//处理消息推送通知
			pushnav(data) {
				let url = '/pages/index/index'
				if (plus.os.name == 'iOS') {
					if (data.payload.url) {
						url = data.payload.url;
					}
				} else {
					if (JSON.parse(data.payload).url) {
						url = JSON.parse(data.payload).url
					}
				}
				setTimeout(() => {
					uni.navigateTo({
						url: url
					})
				}, 1000)
			}
		},
		globalData: {
			ruleconfig: {},
			//内容提示
			alert(msg) {
				uni.showToast({
					title: msg,
					duration: 3000,
					icon: "none",
					mask: true
				})
			},
			//全局请求方法(主要给nvue使用)
			https(data, methods = "GET", isload = false) {
				if (isload) {
					uni.showLoading({
						title: '加载中...',
						mask: true
					})
				}
				return new Promise((resolve, reject) => {
					uni.request({
						url: `${url}` + data.url,
						data: data.param,
						method: methods,
						header: {
							"device": systeminfo.platform, //设备名称(Android,iOS)
							"device-type": systeminfo.model, //设备型号(iphone 9)
							"device-width": systeminfo.windowWidth,
							"device-height": systeminfo.windowHeight,
							"lang": systeminfo.language,
							"req-time": Date.parse(new Date()) / 1000, //请求时间
							"sys-version": systeminfo.system,
							"content-type": "application/json",
							"access-token": uni.getStorageSync('dxzk_access_token'),
							"X-Requested-With": "XMLHttpRequest"
						},
						success(ret) {
							uni.hideLoading();
							if (ret.data.status === 500000) {
								getApp().globalData.expire('登陆状态已过期，请重新登陆！')
								return;
							}
							resolve(ret.data);
						},
						fail(error) {
							uni.hideLoading();
							reject(error);
						}
					})
				})
			},
			expire(msgs) {
				uni.clearStorageSync();
				//添加默认用户信息
				uni.setStorageSync('user_info', {
					level: 0,
					nickname: "天天抽奖用户",
					phone: "",
					avatar: "/static/image/boy.png",
					money: 0,
					integral: 0
				});
				//getApp().globalData.alert(msgs)
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/member/login'
					})
				}, 3000);
			}
		}
	}
</script>
<!--fontSize:18.75px   37.5rpx-->
<style>
	/*每个页面公共css */
	/* #ifndef APP-PLUS-NVUE */
	@import url("/static/iconfont/iconfont.css");
	/* #endif */
</style>
